$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;
    var productPalletInWh = localStorage.getItem("productRopeInWh");
    var productPalletInDp = localStorage.getItem("productRopeInDp");
    var productRopeInTjh = localStorage.getItem("productRopeInTjh");
    $(".details span.good_tjh").html(productRopeInTjh);

    //点击返回箭头 清除 缓存 打开 home.html
    $(".head").on("click", "a.a_back", function () {
        localStorage.removeItem("productRopeInWh");
        localStorage.removeItem("productRopeInDp");
        localStorage.removeItem("productRopeInTjh");
        window.location.href = "storekeeperRope.html";
    });


    $("#goods_id").focus();

    //搜索 批号
    $("#goods_id").on("keyup", function (event) {
        if (event.keyCode == 13) {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            if (!val) {
                pass = 1;
                $alert("批号不可为空！");
                return
            }
            if ($(".content .list-block .goodsList_ul li[attr-data='" + val + "']").length == 1) {
                $alert("不可重复扫描");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return
            }
            var pack_con = JSON.parse(localStorage.getItem("pack_con"));
            var firstBatch = "";
            if (pack_con && pack_con[0]) {
                firstBatch = pack_con[0].cpph;
            }
            // if (pack_con && pack_con.length > 20) {
            //     $("input.cpph").val("");
            //     pass = 1;
            //     return $alert("已达到20件了，请即时提交！");
            // }
            //获取 产品信息  在wd 中
            loading_show("搜索中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "POST",
                url: url + "/app/productinorder/getWareouseForproductPallet",
                data: {
                    whCode: productPalletInWh.split(",")[1],
                    batch: val,
                    firstBatch: firstBatch
                },
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        if (!data) {
                            $alert("此产品不存在！");
                            $("#goods_id").val("").focus();
                        } else {
                            var pack_html = "";
                            pack_html += '<li attr-data = "' + data.batch + '"><div class="row no-gutter">' +
                                '<div class="col-90"><p class="p_none"></p>' +
                                '<p><span>名称:</span><span class="sp goods_cpmc">' +
                                data.inventoryName + '</span></p><p><span>规格:</span><span class="sp goods_ggxh">' +
                                data.inventorySpecification + '</span></p><p><span>排产单:</span><span class="sp goods_cppcd">' +
                                data.workplanNumber + '</span></p><p><span>客户简称:</span><span class="sp goods_cpkh">' +
                                data.customerNameShort + '</span></p><p><span>批号:</span><span class="sp goods_cpph">' +
                                data.batch + '</span></p><p><span>净重:</span><span class="sp goods_cpjz">' +
                                data.availableQuantity + '</span><span>毛重:</span><span class="sp goods_cpmz">' +
                                (accAdd(data.availableQuantity, data.tareQuantity)) + '</span><span>皮重:</span><span class="sp goods_cppz">' +
                                data.tareQuantity + '</span></p></div>' +
                                '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span>' +
                                // '<span class="icon iconfont icon-dayin pull-right"></span>' +
                                '</div>' +
                                '</div></li>';
                            $(".content .list-block .goodsList_ul").append(pack_html);
                            $("#goods_id").val("");
                            badge_text();
                        }
                    } else {
                        console.log(res);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            });
        }
    });

    // li 中的 减号 点击 删除 本条
    $(".content").on("click", ".goodsList_ul li .col-10 .icon-msnui-jian", function () {
        var jianhao = $(this);
        $.confirm('确定删除此货品？', function () {
            jianhao.parent().parent().parent().remove();
            badge_text();
        });
    });

    //点击 清空数据
    $(".foot").on("click", ".add_go", function () {
        $.confirm("确认清空数据吗？", function () {
            $(".content .list-block .goodsList_ul").empty();
            $alert("清空成功");
            badge_text();
            setTimeout(function () {
                $("#goods_id").val("").focus();
            }, 1400);
        }, function () {
            $("#goods_id").val("").focus();
        })
    });

    // 点击 添加完成 生成 产成品入库单
    $(".foot").on("click", ".add_success", function () {
        if ($(".content .goodsList_ul li").length <= 0) {
            return $alert("请添加入库产品！");
        } else {
            $(".mengceng").show();
            $("div.div_ruku").show();
            $("div.div_ruku input").val("");
            $("div.div_ruku input.location").focus();
        }
    });
    $("div.div_ruku .rk_quxiao").on("click", function () {
        $(".mengceng").hide();
        $("div.div_ruku").hide();
        $("#goods_id").val("").focus();
    });
    $("div.div_ruku .rk_queding").on("click", function () {
        if (!$("div.div_ruku input.location").val()) {
            return $alert("请扫描库位")
        } else {
            $.confirm("确认" + $(".content .goodsList_ul li").length + "件产品入库吗？", function () {
                if (pass == 0) {
                    return
                }
                pass = 0;
                loading_show("提交中...");
                //获取 单据信息
                var len = $(".content .goodsList_ul li").length;
                var data = [];
                for (var i = 0; i < len; i++) {
                    data.push({
                        number: $(".content .goodsList_ul li").eq(i).find("span.goods_cpph").text(),
                        weight: $(".content .goodsList_ul li").eq(i).find("span.goods_cpmz").text(),
                        tare_weight: $(".content .goodsList_ul li").eq(i).find("span.goods_cppz").text(),
                        net_weight: $(".content .goodsList_ul li").eq(i).find("span.goods_cpjz").text(),
                        location: $("div.div_ruku input.location").val()
                    })
                }
                //整合数据
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "POST",
                    url: url + "/app/productinorder/saveProductPalletInOrder",
                    data: {
                        whCode: productPalletInWh.split(",")[1],
                        dpCode: productPalletInDp.split(",")[1],
                        palletBatch: productRopeInTjh,
                        data: JSON.stringify(data),
                    },
                    timeout: 60000,
                    success: function (res) {
                        pass = 1;
                        loading_hide();
                        if (res.success) {
                            console.log("产成品数据录入成功");
                            localStorage.removeItem("productRopeInWh");
                            localStorage.removeItem("productRopeInDp");
                            localStorage.removeItem("productRopeInTjh");
                            window.location.href = "storekeeperRope.html";
                        } else {
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        if (error.status == 0) {
                            pass = 1;
                            loading_hide();
                            $alert("请求超时，请重新操作！");
                        } else {
                            pass = 1;
                            loading_hide();
                            console.log("产成品数据录入失败");
                            $alert(error.responseText);
                        }
                    }
                });
            });
        }
    });
    //库位扫描
    $("div.div_ruku input.location").on("keyup", function (event) {
        if (event.keyCode == 13) {
            if (!$("div.div_ruku input.location").val()) {
                return $alert("库位不可为空！");
            } else {
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/productinorder/getLocationForProductPallet',
                    data: {
                        whCode: productPalletInWh.split(",")[1],
                        location: $("div.div_ruku input.location").val()
                    },
                    dataType: 'json',
                    json: 'callback',
                    cache: false,
                    success: function (res) {
                        if (res.success) {
                            var data = res.result;
                            $("div.div_ruku input.location").val(data.location ? data.location : "");
                        } else {
                            setTimeout(function () {
                                $("div.div_ruku input.location").val("").focus();
                            }, 1400);
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        console.log(error);
                        setTimeout(function () {
                            $("input.cpkw").val("").focus();
                        }, 1400);
                        $alert(error.responseText);
                    }
                });
            }
        }
    });

    //点击 某一条 数据  改变 背景颜色
    $(".content").on("click", ".goodsList_ul li", function () {
        $(this).css("background", "#cccccc").siblings("li").css("background", "#ffffff");
    });

    // goodsList中 badge 的值 为 列表中 li 的个数
    function badge_text() {
        var badge_len = $(".content .goodsList_ul li").length;
        //console.log(badge_len);
        var allWeight = 0;
        for (var i = 0; i < badge_len; i++) {
            var weight = $(".content .goodsList_ul li").eq(i).find(".row span.goods_cpjz").html();
            allWeight = accAdd(allWeight, weight);
        }
        $(".details span.good_length").html(badge_len);
        $(".details span.good_weight").html(allWeight);
    }

    badge_text();



});